package com.wj.ch3.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;


/**
 * @program: wj_springBoot
 * @description: Spring Awate
 * @author: wj
 * @create: 2021-10-25 11:19
 **/
@Service
public class AwareService implements BeanNameAware, ResourceLoaderAware {

    private String beanName;
    private ResourceLoader resourceLoader;


    @Override
    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void outputResult(){
        System.out.println("bean 的名称为："+beanName);

        try {
            Resource resource = resourceLoader.getResource("classpath:com/wj/ch3/aware/test.txt");

            String s = IOUtils.toString(resource.getInputStream());
            System.out.println("ResourceLoader 加载的文件内容为： "+s);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
